package com.example.chenduoyun.safe.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.chenduoyun.safe.R;
import com.example.chenduoyun.safe.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author by created chen cloudy 2018/9/21 16:13
 **/

public class ContactListActivity extends Activity {
    private ListView lvContact;
    private List<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
    private MyAdapter mAdapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            mAdapter = new MyAdapter();
            lvContact.setAdapter(mAdapter);
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);
        initUI();
        initData();
    }
class MyAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return contactList.size();
    }

    @Override
    public HashMap<String, String> getItem(int position) {
        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = View.inflate(getApplicationContext(), R.layout.listview_contact, null);
        TextView tv_name = view.findViewById(R.id.tv_name);
        TextView tv_phone = view.findViewById(R.id.tv_phone);
        tv_name.setText(getItem(position).get("name"));
        tv_phone.setText(getItem(position).get("phone"));
        return view;
    }
}
    private void initData() {
        new Thread() {
            @Override
            public void run() {
                ContentResolver contentResolver = getContentResolver();
                Cursor cursor = contentResolver.query(Uri.parse("content://com.android.contacts/raw_contacts")
                        , new String[]{"contact_id"},
                        null, null, null);
                contactList.clear();
                while (cursor.moveToNext()) {
                    String id = cursor.getString(0);
                    Cursor indexCursor = contentResolver.query(Uri.parse("content://com.android.contacts/data"),
                            new String[]{"data1", "mimetype"},
                            "raw_contact_id = ?", new String[]{id}, null);
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    while (indexCursor.moveToNext()) {
                        String data = indexCursor.getString(0);
                        String type = indexCursor.getString(1);
                        if (!TextUtils.isEmpty(data)) {
                            if (type.equals("vnd.android.cursor.item/phone_v2")) {
                                hashMap.put("phone", data);
                            } else if (type.equals("vnd.android.cursor.item/name")) {
                                hashMap.put("name", data);
                            }
                        }
                    }
                    indexCursor.close();
                    contactList.add(hashMap);
                }
                cursor.close();
                mHandler.sendEmptyMessage(0);
            }
        }.start();

    }

    private void initUI() {
        lvContact = (ListView) findViewById(R.id.lv_contact);
        lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mAdapter!=null){
                    HashMap<String, String> cdy = mAdapter.getItem(position);
                    String phone= cdy.get("phone").replace(" ","").replace("-","").trim();
                    Intent intent = new Intent();
                    intent.putExtra("phone",phone);
                    setResult(0,intent);
                    finish();
                }
            }
        });
    }
}
